package controller;

import com.alee.laf.WebLookAndFeel;
import com.alee.laf.panel.WebPanel;
import model.UserModel;
import screens.FriendsListScreen;
import screens.HomeFeedScreen;
import screens.ProfileScreen;
import screens.SearchScreen;
import screens.StatsScreen;
import sql.Db;
import sql.ResultSetHandler;
import view.Toolbar;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

public class App {

    public static App app;
    private JFrame frame;
    private JScrollPane scroll;
    private WebPanel contentPane;
    private Toolbar toolbar;

    private static UserModel currentUser;

    public static UserModel getCurrentUser() {
        return currentUser;
    }

    /**
     * Instantiates app, shows home screen.
     */
    public App() {
        app = this;
        WebLookAndFeel.install();

        this.login();

        // create application level components
        this.createFrame();
        this.createToolbar();
        this.createContentPane();

        // setup frame
        this.frame.pack();
        this.frame.setLocationRelativeTo(null);
        this.frame.setVisible(true);

        // show the home screen
        this.showHome();
    }

    /**
     * Retrieves a user from the database and 'logs in'.
     */
    private void login () {
        Db db = Db.getDb();
        db.executeQuery("SELECT * FROM user WHERE id = 500628070", new ResultSetHandler() {
            public void handleResult(ResultSet rs) {
                try {
                    if (rs.next()) {
                        currentUser = new UserModel(rs);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the window frame.
     */
    public void createFrame() {
        this.frame = new JFrame();
        this.frame.setLayout(new BorderLayout());
    }
    
    /**
     * Create the primary content pane for the application. 
     * This holds all the screens.
     */
    public void createContentPane() {
        // create primary content pane
        contentPane = new WebPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.setBackground(Color.white);
        contentPane.setMargin(20, 100, 0, 100);
        
        // create scroll pane
        scroll = new JScrollPane(contentPane);
        scroll.setPreferredSize(new Dimension(820, 480));
        scroll.setBorder(null);
        
        // add to frame
        this.frame.add(scroll, BorderLayout.CENTER);
    }

    /**
     * Create global application toolbar.
     */
    public void createToolbar() {
        toolbar = new Toolbar();
        this.frame.add(toolbar, BorderLayout.NORTH);
        
        // wire up event listeners
        toolbar.homeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                showHome();
            } 
        });
        toolbar.friendsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                showFriends();
            } 
        });
        toolbar.statsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                showStats();
            }
        });
        toolbar.searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                showSearch();
            }
        });

    }
    
    /**
     * Show home screen
     */
    public void showHome() {
        contentPane.removeAll();
        HomeFeedScreen s = new HomeFeedScreen();
        contentPane.add(s);
        contentPane.revalidate();
    }
    
    /**
     * Show friends screen
     */
    public void showFriends() {
        contentPane.removeAll();
        FriendsListScreen s = new FriendsListScreen();
        contentPane.add(s);
        contentPane.revalidate();
    }
    
    /**
     * Show events screen
     */
    public void showStats() {
        contentPane.removeAll();
        StatsScreen s = new StatsScreen();
        contentPane.add(s);
        contentPane.revalidate();
    }

    /**
     * Show search
     */
    public void showSearch() {
        contentPane.removeAll();
        SearchScreen s = new SearchScreen();
        contentPane.add(s);
        contentPane.revalidate();
    }

    /**
     * Show a profile
     * @param targetId targetId to show
     */
    public void showProfile(String targetId) {
        contentPane.removeAll();
        ProfileScreen s = new ProfileScreen(targetId);
        contentPane.add(s);
        contentPane.revalidate();
    }

    /**
     * Main method to start app
     */
    public static void main(String[] args) {
        new App();
    }
}
